//
//  CommitOrderViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/8/28.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "CommitOrderViewController.h"
#import "AddressViewCell.h"
#import "ParameterCell.h"
#import "AddressManageViewController.h"
#import "AddressManagerModel.h"
#import "PayViewController.h"

@interface CommitOrderViewController ()<UITableViewDelegate, UITableViewDataSource,passValueDelegate>

@property (nonatomic, strong)AddressManageViewController *addressManageViewController;
@property (nonatomic, strong)AddressManagerModel *addressDataModel;
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UILabel *totalLabel;
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, assign) NSMutableArray *dataArray;

@end

@implementation CommitOrderViewController

- (void)viewDidLoad {
    _addressManageViewController = [[AddressManageViewController alloc] init];
    _addressManageViewController.delegate = self;
    _addressManageViewController.isSelect = YES;
    
    [self setTitle:@"提交订单"];
    [super viewDidLoad];
    [self setTableView];
    [self setFooter];
    [self getData];
    // Do any additional setup after loading the view.
}

-(void)setTableView{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    [self.view addSubview:self.tableView];
}

-(void)setFooter{
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-ScreenX375(49)-NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(49))];
    
    _totalLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(17), ScreenX375(150), ScreenX375(12))];
    _totalLabel.text = [NSString stringWithFormat:@"总金额￥%@",_price];
    _totalLabel.font = Kfont(16);
    _totalLabel.textColor = REXADECIMALCOLOR(@"#ff235d");
    _totalLabel.textAlignment = NSTextAlignmentLeft;
    [baseView addSubview:_totalLabel];
    
    UIButton *payBtn =[[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(275), ScreenX375(0), ScreenX375(100), ScreenX375(49))];
    payBtn.titleLabel.font = Kfont(16);
    [payBtn setTitle:@"去支付" forState:UIControlStateNormal];
    [payBtn setBackgroundImage:ImageWithName(@"按钮背景") forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(doneClicked) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:payBtn];
    
    [self.view addSubview:baseView];
}

-(void)doneClicked
{
    PayViewController *vc = [[PayViewController alloc] init];
    vc.picUrl = _picUrl;
    vc.proName = _proName;
    vc.proId = _proId;
    vc.price = _price;
    vc.recName = _addressDataModel.recName;
    vc.recPhone = _addressDataModel.recPhone;
    vc.recArea = _addressDataModel.recArea;
    vc.recCity = _addressDataModel.recCity;
    vc.recAddress = _addressDataModel.recAddress;
    vc.recProvince = _addressDataModel.recProvince;
    vc.isCopyRight = _isCopyRight;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma tableviewdelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return 5;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 & indexPath.row == 0) {
        AddressViewCell *cell = [AddressViewCell cellWithTableView:tableView andIndexPath:indexPath];
        [cell initWithModel:_addressDataModel];
        return cell;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        static NSString *cellID = @"pictureCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor whiteColor];
        }
        _pictureView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(12), ScreenX375(343), ScreenX375(343))];
        _pictureView.layer.cornerRadius = 6.0;
        [_pictureView sd_setImageWithURL:[NSURL URLWithString:_picUrl] placeholderImage:nil options:SDWebImageRetryFailed];
        _pictureView.clipsToBounds = YES;
        [cell addSubview:_pictureView];
        
        return cell;
    }else{
        NSArray *titleText = [[NSArray alloc] initWithObjects:@"",@"款式:",@"尺码:",@"定制时间:",@"金额:", nil];
        NSArray *parameterText = [[NSArray alloc] initWithObjects:@"",_type,_size,_makeTime,[NSString stringWithFormat:@"%@元",_price], nil];
        ParameterCell *cell = [ParameterCell cellWithTableView:tableView andIndexPath:indexPath];
        
        [cell setTitleLabelText:[titleText objectAtIndex:indexPath.row]];
        [cell setParameterLabelText:[parameterText objectAtIndex:indexPath.row]];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return ScreenX375(87);
    }else if (indexPath.section == 1 && indexPath.row == 0){
        return ScreenX375(355);
    }else{
        return ScreenX375(55);
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return ScreenX375(8);
    }else{
        return 0;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if(indexPath.section == 0){
            [self.navigationController pushViewController:_addressManageViewController animated:YES];
        }
    }
}

- (void)getData {
    NSDictionary *parameter = @{@"id":kNSUDefaultReadKey(@"userId")};
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getUserAddress",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            [self.dataArray removeAllObjects];
            NSError *error = nil;
            self.dataArray = [NSMutableArray array];
            for (NSDictionary *dict in successResponse[@"data"][@"list"]) {
                AddressManagerModel *model = [[AddressManagerModel alloc] initWithDictionary:dict error:&error];
                [self.dataArray addObject:model];
            }
            self.addressDataModel = [self.dataArray objectAtIndex:0];
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)passValue:(AddressManagerModel *)model{
    _addressDataModel = model;
    [self.tableView reloadData];
}
@end
